package com.wonders.qxmanager.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;

import com.wonders.qxmanager.base.WidgetPage;

public class PageUIHelper {

	public static class Page implements WidgetPage {
		/**
		 * Grid的分页帮助类
		 * 
		 * @param pageNo
		 *            第几页
		 * @param pageSize
		 *            每页大小
		 * @param sort
		 *            排序的字段
		 * @param dir
		 *            排序字段的方向
		 * @param suffix
		 *            SQL的额外条件
		 */
		protected Page(String pageNo, String pageSize, String sort, String dir,
				String suffix) {
			try {
				this.pageSize = Integer.parseInt(pageSize);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (this.pageSize > MAX_PAGE_SIZE) {
				this.pageSize = MAX_PAGE_SIZE;
			}
			try {
				currentPage = Integer.parseInt(pageNo);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		@Override
		public int getCurrentPage() {
			return currentPage;
		}

		@Override
		public String getOrder() {
			return order;
		}

		@Override
		public int getPageSize() {
			return pageSize;
		}

		@Override
		public String getSuffix() {
			if (suffix.isEmpty() && !order.isEmpty())
				return this.order;
			return suffix;
		}

		@Override
		public int getStartOfPage() {
			int startIndex = (currentPage - 1) * pageSize + 1;
			if (startIndex < 1) {
				startIndex = 1;
			}
			return startIndex;
		}

		private int currentPage = 1;
		private String order = "";

		private int pageSize = Integer.MAX_VALUE / 2;
		private String suffix = "";
	}

	/**
	 * 解析请求的参数构建分页信息
	 * 
	 * @param req
	 *            请求的参数中解析项为：
	 *            START_PARAM,LIMIT_PARAM,SORT_PARAM,DIR_PARAM,SUFFIX_PARAM
	 * @return
	 */
	public static Page getPage(HttpServletRequest req) {
		return new Page(req.getParameter(PAGE_NO), req.getParameter(PAGE_SIZE),
				req.getParameter(SORT_PARAM), req.getParameter(DIR_PARAM),
				req.getParameter(SUFFIX_PARAM));
	}

	/**
	 * 向response输出结果
	 * 
	 * @param res
	 * @param result
	 *            结果
	 * @throws IOException
	 */
	public static void writeResponse(HttpServletResponse res, String result)
			throws IOException {
		writeResponse(res, result, SystemConstant.DEFAULT_CHARSET);
	}

	public static void writeFormResponse(HttpServletResponse res,
			String result, String charset) throws IOException {
		result = StringEscapeUtils.escapeHtml(result);
		res.setContentType("text/html;charset="
				+ SystemConstant.DEFAULT_CHARSET);
		res.setCharacterEncoding(SystemConstant.DEFAULT_CHARSET);
		res.getWriter().write(result);
	}

	/**
	 * 向response输出结果，若前台提交形式为IFrame而非ajax，则需要转义html代码
	 * 
	 * @param res
	 * @param result
	 *            结果
	 * @throws IOException
	 */
	public static void writeFormResponse(HttpServletResponse res, String result)
			throws IOException {
		writeFormResponse(res, result, SystemConstant.DEFAULT_CHARSET);
	}

	/**
	 * 向response输出结果，客户端直接得到json
	 * 
	 * @param res
	 * @param result
	 *            结果
	 * @throws IOException
	 */
	public static void writeJsonResponse(HttpServletResponse res, String result)
			throws IOException {
		writeJsonResponse(res, result, SystemConstant.DEFAULT_CHARSET);
	}

	/**
	 * 向response输出结果
	 * 
	 * @param res
	 * @param result
	 *            结果
	 * @param charset
	 *            编码
	 * @throws IOException
	 */
	public static void writeResponse(HttpServletResponse res, String result,
			String charset) throws IOException {
		res.setContentType("text/plain;charset=" + charset);
		res.setCharacterEncoding(charset);
		res.getWriter().write(result);
	}

	/**
	 * 向response输出json结果
	 * 
	 * @param res
	 * @param result
	 *            结果
	 * @param charset
	 *            编码
	 * @throws IOException
	 */
	public static void writeJsonResponse(HttpServletResponse res,
			String result, String charset) throws IOException {
		res.setContentType("application/json; charset=" + charset);
		res.setCharacterEncoding(charset);
		res.getWriter().write(result);
	}
	
	public static String toString(PageArrayList<?> pageArrayList) {
		net.sf.json.JSONObject jso = new net.sf.json.JSONObject();
		int total = 0;
		net.sf.json.JSONArray jsa = null;
		if (pageArrayList != null) {
			jsa = net.sf.json.JSONArray.fromObject(pageArrayList);
		} else {
			jsa = new net.sf.json.JSONArray();
		}
		total = pageArrayList.getTotalItemCount();
		jso.put("total", total);
		jso.put("rows", jsa.toString());
		return jso.toString();
	}


	/**
	 * 查询参数：排序方向
	 */
	public static final String DIR_PARAM = "order";

	/**
	 * 最大每页大小
	 */
	public static final int MAX_PAGE_SIZE = 100;

	/**
	 * 查询参数：第几页
	 */
	public static final String PAGE_NO = "page";

	/**
	 * 查询参数：每页大小
	 */
	public static final String PAGE_SIZE = "rows";

	/**
	 * 查询参数：排序字段
	 */
	public static final String SORT_PARAM = "sort";

	/**
	 * 查询参数：SQL额外条件
	 */
	public static final String SUFFIX_PARAM = "q.s";

}
